/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package queenb.web.controller;

import com.beyu.internalws.AgentSmsApi_Service;
import com.fibo.internalws.Service;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import queenb.net.entitybean.M000;
import queenb.net.entitybean.M020;
import queenb.net.entitybean.N100;
import queenb.web.crm.controller.V030controller;
import queenb.web.store.M000Store;
;
import queenb.web.store.M020Store;
import queenb.web.store.V050Store;
import queenb.web.util.SMSUTIL;
import queenb.web.util.SessionName;
import queenb.web.util.SessionProvider;
import queenb.web.util.Util;
import queenb.web.store.M020Store;
import queenb.web.store.V050Store;
import queenb.web.util.SMSUTIL;
import queenb.web.util.SessionName;
import queenb.web.util.SessionProvider;
import queenb.web.util.Util;
import queenb.web.store.M020Store;
import queenb.web.store.V050Store;
import queenb.web.util.SMSUTIL;
import queenb.web.util.SessionName;
import queenb.web.util.SessionProvider;
import queenb.web.util.Util;
import queenb.web.store.M020Store;
import queenb.web.store.V050Store;
import queenb.web.util.SMSUTIL;
import queenb.web.util.SessionName;
import queenb.web.util.SessionProvider;
import queenb.web.util.Util;
import queenb.web.store.M020Store;
import queenb.web.store.V050Store;
import queenb.web.util.SMSUTIL;
import queenb.web.util.SessionName;
import queenb.web.util.SessionProvider;
import queenb.web.util.Util;
import queenb.web.store.M020Store;
import queenb.web.store.V050Store;
import queenb.web.util.PackageContants;
import queenb.web.util.SMSUTIL;
import queenb.web.util.SessionName;
import queenb.web.util.SessionProvider;
import queenb.web.util.Util;
import queenb.web.store.M020Store;
import queenb.web.store.V050Store;
import queenb.web.util.SMSUTIL;
import queenb.web.util.SessionName;
import queenb.web.util.SessionProvider;
import queenb.web.util.Util;
import queenb.web.store.M020Store;
import queenb.web.store.V050Store;
import queenb.web.util.SMSUTIL;
import queenb.web.util.SessionName;
import queenb.web.util.SessionProvider;
import queenb.web.util.Util;
import queenb.web.store.M020Store;
import queenb.web.store.V050Store;
import queenb.web.util.SMSUTIL;
import queenb.web.util.SessionName;
import queenb.web.util.SessionProvider;
import queenb.web.util.Util;
import queenb.web.store.M020Store;
import queenb.web.store.V050Store;
import queenb.web.util.SMSUTIL;
import queenb.web.util.SessionName;
import queenb.web.util.SessionProvider;
import queenb.web.util.Util;
import queenb.web.store.M020Store;
import queenb.web.store.V050Store;
import queenb.web.util.SMSUTIL;
import queenb.web.util.SessionName;
import queenb.web.util.SessionProvider;
import queenb.web.util.Util;
import queenb.web.store.M020Store;
import queenb.web.store.V050Store;
import queenb.web.util.SMSUTIL;
import queenb.web.util.SessionName;
import queenb.web.util.SessionProvider;
import queenb.web.util.Util;
import queenb.web.store.M020Store;
import queenb.web.store.V050Store;
import queenb.web.util.SMSUTIL;
import queenb.web.util.SessionName;
import queenb.web.util.SessionProvider;
import queenb.web.util.Util;
import queenb.web.store.M020Store;
import queenb.web.store.V050Store;
import queenb.web.util.SMSUTIL;
import queenb.web.util.SessionName;
import queenb.web.util.SessionProvider;
import queenb.web.util.Util;
import queenb.web.store.M020Store;
import queenb.web.store.V050Store;
import queenb.web.util.PackageContants;
import queenb.web.util.SMSUTIL;
import queenb.web.util.SessionName;
import queenb.web.util.SessionProvider;
import queenb.web.util.Util;
import queenb.web.store.M020Store;
import queenb.web.store.V050Store;
import queenb.web.util.SMSUTIL;
import queenb.web.util.SessionName;
import queenb.web.util.SessionProvider;
import queenb.web.util.Util;
import queenb.web.store.M020Store;
import queenb.web.store.V050Store;
import queenb.web.util.SMSUTIL;
import queenb.web.util.SessionName;
import queenb.web.util.SessionProvider;
import queenb.web.util.Util;
import queenb.web.store.M020Store;
import queenb.web.store.V050Store;
import queenb.web.util.SMSUTIL;
import queenb.web.util.SessionName;
import queenb.web.util.SessionProvider;
import queenb.web.util.Util;
import queenb.web.store.M020Store;
import queenb.web.store.V050Store;
import queenb.web.util.SMSUTIL;
import queenb.web.util.SessionName;
import queenb.web.util.SessionProvider;
import queenb.web.util.Util;
import queenb.web.store.M020Store;
import queenb.web.store.V050Store;
import queenb.web.util.SMSUTIL;
import queenb.web.util.SessionName;
import queenb.web.util.SessionProvider;
import queenb.web.util.Util;
import queenb.web.store.M020Store;
import queenb.web.store.V050Store;
import queenb.web.util.SMSUTIL;
import queenb.web.util.SessionName;
import queenb.web.util.SessionProvider;
import queenb.web.util.Util;
import queenb.web.store.M020Store;
import queenb.web.store.V050Store;
import queenb.web.util.SMSUTIL;
import queenb.web.util.SessionName;
import queenb.web.util.SessionProvider;
import queenb.web.util.Util;
import queenb.web.store.M020Store;
import queenb.web.store.V050Store;
import queenb.web.util.SMSUTIL;
import queenb.web.util.SessionName;
import queenb.web.util.SessionProvider;
import queenb.web.util.Util;
import queenb.web.store.M020Store;
import queenb.web.store.V050Store;
import queenb.web.util.PackageContants;
import queenb.web.util.SMSUTIL;
import queenb.web.util.SessionName;
import queenb.web.util.SessionProvider;
import queenb.web.util.Util;
import queenb.web.store.M020Store;
import queenb.web.store.V050Store;
import queenb.web.util.SMSUTIL;
import queenb.web.util.SessionName;
import queenb.web.util.SessionProvider;
import queenb.web.util.Util;
import queenb.web.store.M020Store;
import queenb.web.store.V050Store;
import queenb.web.util.SMSUTIL;
import queenb.web.util.SessionName;
import queenb.web.util.SessionProvider;
import queenb.web.util.Util;
import queenb.web.store.M020Store;
import queenb.web.store.V050Store;
import queenb.web.util.SMSUTIL;
import queenb.web.util.SessionName;
import queenb.web.util.SessionProvider;
import queenb.web.util.Util;
import queenb.web.store.M020Store;
import queenb.web.store.V050Store;
import queenb.web.util.SMSUTIL;
import queenb.web.util.SessionName;
import queenb.web.util.SessionProvider;
import queenb.web.util.Util;
import queenb.web.store.M020Store;
import queenb.web.store.V050Store;
import queenb.web.util.SMSUTIL;
import queenb.web.util.SessionName;
import queenb.web.util.SessionProvider;
import queenb.web.util.Util;
import queenb.web.store.M020Store;
import queenb.web.store.V050Store;
import queenb.web.util.SMSUTIL;
import queenb.web.util.SessionName;
import queenb.web.util.SessionProvider;
import queenb.web.util.Util;
import queenb.web.store.M020Store;
import queenb.web.store.V050Store;
import queenb.web.util.SMSUTIL;
import queenb.web.util.SessionName;
import queenb.web.util.SessionProvider;
import queenb.web.util.Util;
import queenb.web.store.M020Store;
import queenb.web.store.V050Store;
import queenb.web.util.SMSUTIL;
import queenb.web.util.SessionName;
import queenb.web.util.SessionProvider;
import queenb.web.util.Util;
import queenb.web.store.M020Store;
import queenb.web.store.V050Store;
import queenb.web.util.PackageContants;
import queenb.web.util.SMSUTIL;
import queenb.web.util.SessionName;
import queenb.web.util.SessionProvider;
import queenb.web.util.Util;
import queenb.web.store.M020Store;
import queenb.web.store.V050Store;
import queenb.web.util.SMSUTIL;
import queenb.web.util.SessionName;
import queenb.web.util.SessionProvider;
import queenb.web.util.Util;
import queenb.web.store.M020Store;
import queenb.web.store.V050Store;
import queenb.web.util.SMSUTIL;
import queenb.web.util.SessionName;
import queenb.web.util.SessionProvider;
import queenb.web.util.Util;
import queenb.web.store.M020Store;
import queenb.web.store.V050Store;
import queenb.web.util.SMSUTIL;
import queenb.web.util.SessionName;
import queenb.web.util.SessionProvider;
import queenb.web.util.Util;import queenb.web.store.M020Store;
import queenb.web.store.V050Store;
import queenb.web.util.SMSUTIL;
import queenb.web.util.SessionName;
import queenb.web.util.SessionProvider;
import queenb.web.util.Util;
import queenb.web.store.M020Store;
import queenb.web.store.V050Store;
import queenb.web.util.SMSUTIL;
import queenb.web.util.SessionName;
import queenb.web.util.SessionProvider;
import queenb.web.util.Util;
import queenb.web.store.M020Store;
import queenb.web.store.V050Store;
import queenb.web.util.SMSUTIL;
import queenb.web.util.SessionName;
import queenb.web.util.SessionProvider;
import queenb.web.util.Util;
import queenb.web.store.M020Store;
import queenb.web.store.V050Store;
import queenb.web.util.SMSUTIL;
import queenb.web.util.SessionName;
import queenb.web.util.SessionProvider;
import queenb.web.util.Util;
import queenb.web.store.M020Store;
import queenb.web.store.V050Store;
import queenb.web.util.SMSUTIL;
import queenb.web.util.SessionName;
import queenb.web.util.SessionProvider;
import queenb.web.util.Util;
import queenb.web.store.M020Store;
import queenb.web.store.V050Store;
import queenb.web.util.PackageContants;
import queenb.web.util.SMSUTIL;
import queenb.web.util.SessionName;
import queenb.web.util.SessionProvider;
import queenb.web.util.Util;
import queenb.web.store.M020Store;
import queenb.web.store.V050Store;
import queenb.web.util.SMSUTIL;
import queenb.web.util.SessionName;
import queenb.web.util.SessionProvider;
import queenb.web.util.Util;
import queenb.web.store.M020Store;
import queenb.web.store.V050Store;
import queenb.web.util.SMSUTIL;
import queenb.web.util.SessionName;
import queenb.web.util.SessionProvider;
import queenb.web.util.Util;
import queenb.web.store.M020Store;
import queenb.web.store.V050Store;
import queenb.web.util.SMSUTIL;
import queenb.web.util.SessionName;
import queenb.web.util.SessionProvider;
import queenb.web.util.Util;
import queenb.web.store.M020Store;
import queenb.web.store.V050Store;
import queenb.web.util.SMSUTIL;
import queenb.web.util.SessionName;
import queenb.web.util.SessionProvider;
import queenb.web.util.Util;
import queenb.web.store.M020Store;
import queenb.web.store.V050Store;
import queenb.web.util.SMSUTIL;
import queenb.web.util.SessionName;
import queenb.web.util.SessionProvider;
import queenb.web.util.Util;
import queenb.web.store.M020Store;
import queenb.web.store.V050Store;
import queenb.web.util.SMSUTIL;
import queenb.web.util.SessionName;
import queenb.web.util.SessionProvider;
import queenb.web.util.Util;
import queenb.web.store.M020Store;
import queenb.web.store.V050Store;
import queenb.web.util.SMSUTIL;
import queenb.web.util.SessionName;
import queenb.web.util.SessionProvider;
import queenb.web.util.Util;
import queenb.web.store.M020Store;
import queenb.web.store.V050Store;
import queenb.web.util.SMSUTIL;
import queenb.web.util.SessionName;
import queenb.web.util.SessionProvider;
import queenb.web.util.Util;
import queenb.web.store.M020Store;
import queenb.web.store.V050Store;
import queenb.web.util.PackageContants;
import queenb.web.util.SMSUTIL;
import queenb.web.util.SessionName;
import queenb.web.util.SessionProvider;
import queenb.web.util.Util;
import queenb.web.store.M020Store;
import queenb.web.store.V050Store;
import queenb.web.util.SMSUTIL;
import queenb.web.util.SessionName;
import queenb.web.util.SessionProvider;
import queenb.web.util.Util;
import queenb.web.store.M020Store;
import queenb.web.store.V050Store;
import queenb.web.util.SMSUTIL;
import queenb.web.util.SessionName;
import queenb.web.util.SessionProvider;
import queenb.web.util.Util;
import queenb.web.store.M020Store;
import queenb.web.store.V050Store;
import queenb.web.util.SMSUTIL;
import queenb.web.util.SessionName;
import queenb.web.util.SessionProvider;
import queenb.web.util.Util;

/**
 *
 * @author Nguyen Hung
 */


public class templateController extends QueenbController {

    private int fv150;// id chuyen bay
    private String phone;// so dt gui sinh nhat
    private String hoten;// ho ten gui sinh nhat
    private int fk100;// íd cong ty
    private int fn850;// phong ban cua nhan vien
    private int pm020;
    private int pc050;
    private int fv050;
    private int fn100;
    private int fm000;
    private int fm010;
    private String mv025;// status
    private Date md023;
    private String namepage;// tro ve page can tra
    private int mn026;// fillter theo SMS
    private String nameTour;// ten Tour
    private String dateTour;// ngay Tour
    private String messageSMS;// thong bao SMS
    private Date md024;
    private String cv051;
    private String cv052;// send content sms
    private List<M020> list = new ArrayList<M020>();
    private M020Store m020Store;
    private List<N100> listSMSFinal = new ArrayList<N100>();// danh sach gui sms
    private V030controller v030Controller;
    private V050Store v050Store;
    private int flag;//phân biêt SMS nhan tin theo hinh thuc nao
    private int countchar = 320;// ky tu sms
    private int numberSMS;// so nguoi được send
    private int numberflag;// phan biet sned sms tu dau    
    private int group;//= 1 send group// 0 send person
    private String page;
    private String addnumbersms;// sdt them
    private int flagncc = 1;//Nhà cung cap 

    public templateController() {
        m020Store = new M020Store();
        Object objPage = getRequest().getParameter("page");
        Object action = getRequest().getParameter("action");
        Object objGroup = getRequest().getParameter("group");
        Object phonenumber = null;
        Object fn100Number = null;
        Object hotenNumber = null;
        Object numberphonecheck = getRequest().getParameter("");
        Object objFk100 = getRequest().getParameter("fk100");
        if (objFk100 != null) {
            fk100 = Util.parseInt(objFk100);
        }

        if (action != null) {
            if (action.equals("sendSMSbir")) {
                phonenumber = getRequest().getParameter("phone");
                if (phonenumber != null) {
                    phone = phonenumber.toString();
                }
                fn100Number = getRequest().getParameter("fn100");
                if (fn100Number != null) {
                    fn100 = Util.parseInt(fn100Number);
                }
                hotenNumber = getRequest().getParameter("hoten");
                if (hotenNumber != null) {
                    hoten = hotenNumber.toString();
                    SessionProvider.setSessionMapValue("HOTENSENDSMS", hoten);
                }
            } else if (action.equals("customer4tour")) {
                try {
                    setFlag(1);
                    setNumberflag(100);
                    v050Store = new V050Store();
                    v030Controller = new V030controller();
                    fv050 = v030Controller.getFV050();
//                    if (fv050 != 0) {
//                        List<V050> listitemV050 = v050Store.vcurpks_listoftabv050new(getLogin().getModule(), getLogin().getCompanyNo(), fv050, 0, 0, null, null, null, 0, 50, 0, 0, getLogin().getUserName());
//                        if (listitemV050 != null) {
//                            nameTour = listitemV050.get(0).getVv067();
//                            dateTour = listitemV050.get(0).getVd073().toString();
//                            if (nameTour != null) {
//                                SessionProvider.setSessionMapValue("NAMETOURSENDSMS", nameTour);
//                            }
//                            if (dateTour != null) {
//
//                                dateTour = dateTour.substring(8, 10) + "/" + dateTour.substring(5, 7) + "/" + dateTour.substring(0, 4);
//                                SessionProvider.setSessionMapValue("DATETOURSENDSMS", dateTour);
//                            }
//                        }
//                    }
                } catch (Exception e) {
                }
            } else if (action.equals("customerexpand")) {
                try {
                    setNumberflag(101);
                } catch (Exception e) {
                }

            } else if (action.equals("sendSMSfail")) {
                phonenumber = getRequest().getParameter("phone");
                if (phonenumber != null) {
                    phone = phonenumber.toString();
                }
                Object p = getRequest().getParameter("p");
                if (p != null) {
                    namepage = p.toString();
                }
                Object grounp = getRequest().getParameter("group");
                if (grounp != null) {
                    group = Util.parseInt(grounp);
                }
                fn100Number = getRequest().getParameter("fn100");
                if (fn100Number != null) {
                    fn100 = Util.parseInt(fn100Number);
                }
                Object pm020Number = getRequest().getParameter("pm020");
                if (pm020Number != null) {
                    pm020 = Util.parseInt(pm020Number);
                    //   SessionProvider.setSessionMapValue("HOTENSENDSMS", hoten);


                }
                try {
                    M020Store m020Store = new M020Store();
                    V030controller v030 = new V030controller();
                    List<M020> listM020 = m020Store.mcurpks_listoftabm020(getLogin().getModule(), getLogin().getCompanyNo(), pm020, v030.getFV050(), fn100,"B", null, null, 0, getLogin().getUserName());
                    if (listM020 != null) {
                        cv051 = listM020.get(0).getMv022();
                        cv052 = listM020.get(0).getMv021();
                        mn026 = listM020.get(0).getMn026();
                        pc050 = listM020.get(0).getFm000();
                    }
                } catch (Exception e) {
                }
            }
        }
        if (objPage != null) {
            page = objPage.toString();
            if (objGroup != null) {

                //lay sdt, fn100, hoten
                setGroup(Integer.parseInt(objGroup.toString()));
                if (Integer.parseInt(objGroup.toString()) == 1) {//send 1 ng
                    setNumberSMS(1);
                    phonenumber = getRequest().getParameter("phone");
                    if (phonenumber != null) {
                        phone = phonenumber.toString();
                    }
                    fn100Number = getRequest().getParameter("fn100");
                    if (fn100Number != null) {
                        fn100 = Util.parseInt(fn100Number);
                    }
                    hotenNumber = getRequest().getParameter("hoten");
                    if (hotenNumber != null) {
                        hoten = hotenNumber.toString();
                        SessionProvider.setSessionMapValue("HOTENSENDSMS", hoten);
                    }
                } else {//send group
                }
                //gui sms tu tour > lay them ten tour/ ngay di
                if (page.equals("customer4tour.jsp")) {

                    Object objTourName = getRequest().getParameter("tourname");
                    if (objTourName != null) {
                        nameTour = objTourName.toString();
                    }
                    Object objTourDate = getRequest().getParameter("tourdate");
                    if (objTourDate != null) {
                        dateTour = objTourDate.toString();
                    }
                } else if (page.equals("sendSMSitemCompany")) {
                }
            }

        }


    }

    public void onchangeTemplate() {
        try {
             System.out.println("---AAAAAAAAAA");
            if (getPc050() != 0) {
                System.out.println("---AAAAAAAAAA");
                M000Store m000Store = new M000Store();
                List<M000> listM000 = m000Store.mcurpks_listoftabm000(getLogin().getModule(), getLogin().getCompanyNo(), pc050, getLogin().getUserName());
                setCv051(listM000.get(0).getMv002());
                if (getPc050() == 4) {
                    if (SessionProvider.getSessionMapValue(SessionName.TOUR_NAME_UNI) != null) {
                        nameTour = SessionProvider.getSessionMapValue(SessionName.TOUR_NAME_UNI).toString();
                        System.out.println("----------TEN TOUR KO DAU----------" + nameTour);
                    }

                    if (SessionProvider.getSessionMapValue(SessionName.TOUR_DATE) != null) {
                        dateTour = SessionProvider.getSessionMapValue(SessionName.TOUR_DATE).toString();
                    }
                    dateTour = dateTour.substring(8, 10) + "/" + dateTour.substring(5, 7) + "/" + dateTour.substring(0, 4);
                    String templea4 = listM000.get(0).getMv002();
                    String change1 = templea4.replace("{P1}", nameTour);
                    String change2 = change1.replace("{P2}", dateTour);
                    setCv051(change2);
                } else if (getPc050() == 6) {
                    if (SessionProvider.getSessionMapValue("HOTENSENDSMS") != null) {
                        hoten = SessionProvider.getSessionMapValue("HOTENSENDSMS").toString();
                    }
                    String templea4 = listM000.get(0).getMv002();
                    String change1 = templea4.replace("{P1}", hoten);
                    setCv051(change1);
                    setCountchar(change1.length());
                }
            }

        } catch (Exception e) {
        }
    }

    public void saveTemplateSMSTour() {
        try {

            V030controller v030controller = new V030controller();
            String redirect =null;
            if(namepage.equals("")){
                  redirect = "customer4tour.jsp?status=T&pv050=" + v030controller.getFV050() + "&action=delSS&linkA00=" + Util.convertMD5(getRequest().getRequestURI());
            }else{
                  redirect = "historySMS.jsp?" + Util.convertMD5(getRequest().getRequestURI());
            }
           
            fv050 = v030controller.getFV050();
            if (cv051.length() != 0) {
                SMSController sController = new SMSController();
                System.out.println("-----------THÊM------------" + addnumbersms + addnumbersms.length());
                if (addnumbersms.length() > 0) {
                    String[] stringsms = addnumbersms.toString().split(",");
                    if (stringsms != null) {
                        for (int i = 0; i < stringsms.length; i++) {
                              if (flagncc == 2) {
                                  messageSMS = sendSmSUtil(cv051, stringsms[i].toString(),redirect);
                             }else{
                                  messageSMS = sendSmSUtilFibo(stringsms[i].toString(),cv051,redirect);
                             }
                           
                            int count = m020Store.stdm_tools_inserttabm020(pm020, cv052, cv051, "B",Util.toSQLDate(new Date()),
                                    Util.toSQLDate(new Date()), messageSMS, mn026, fm000, fv050, fn100,stringsms[i].toString(),"", getLogin().getUserName());
                            System.out.println(messageSMS+count + "----------AAAAAAAAAAAAA----------" + stringsms[i].toString());
                        }
                    }
                }
                if (group == 1) {
                    if (cv051.length() > 0 && phone.length() > 0) {
                        System.out.println("---------VAO HAM GUI SMS-------1NGUOI---" + cv051 + phone);
                        if (flagncc == 2) {
                                  messageSMS = SMSUTIL.sendSmSUtil(cv051, phone);
                             }else{
                                  messageSMS = SMSUTIL.sendSmSUtilFibo(phone,cv051);
                             }
                    }
                    int count = m020Store.stdm_tools_inserttabm020(pm020, cv052, cv051,"B", Util.toSQLDate(new Date()),
                            Util.toSQLDate(new Date()), messageSMS, mn026, fm000, fv050, fn100,phone, "",getLogin().getUserName());
                    if (messageSMS.equals("SMS SUCCESS")) {
                        showGlobalReport(true, getResourceString("datasendsmssuccess"));
                        sendRedirectUrlTab(redirect);
                    } else {
                        showGlobalReport(true, getResourceString("datasendsmsUnsuccess"));
                        sendRedirectUrlTab(redirect);
                    }
                } else {
                    if (sController.getListSMS().size() > 0) {
                        listSMSFinal = sController.getListSMS();
                        for (int i = 0; i < listSMSFinal.size(); i++) {
                            if (listSMSFinal.get(i).getNv121() == null) {
                                System.out.println("-----------DT null sẽ vào đây-------" + listSMSFinal.get(i).getNv121());
                                SGTSendSMS(cv052, cv051, listSMSFinal.get(i).getNv121(), fv050, listSMSFinal.get(i).getPn100(), redirect, mn026, pc050);
                            } else {
                                if (flagncc == 2) {
                                   
                                    System.out.println("----BEYU-----------CAC THAM SO-------TOUR---" + cv052 + "," + cv051 + "," + listSMSFinal.get(i).getNv121() + "," + fv050 + "," + listSMSFinal.get(i).getPn100() + "," + redirect + "," + mn026 + "," + pc050);
                                    SGTSendSMS(cv052, cv051, listSMSFinal.get(i).getNv121(), fv050, listSMSFinal.get(i).getPn100(), redirect, mn026, pc050);
                                } else {
                                    System.out.println("------FIBO---------CAC THAM SO-------TOUR---" + cv052 + "," + cv051 + "," + listSMSFinal.get(i).getNv121() + "," + fv050 + "," + listSMSFinal.get(i).getPn100() + "," + redirect + "," + mn026 + "," + pc050);
                                    SGTSendSMSFibo(cv052, cv051, listSMSFinal.get(i).getNv121(), fv050, listSMSFinal.get(i).getPn100(), redirect, mn026, pc050);
                                }

                            }
                        }
                    }
                }
            } else {
                showGlobalReport(true, getResourceString("Nội dung trống!!!!"));
                sendRedirect(redirect);
            }

        } catch (Exception e) {
        }
    }

    public void sgtSendSMS() {
        try {
            String redirect = null;
            SMSController sController = new SMSController();
            if (cv051.length() != 0) {
                System.out.println("---------CONTENT---" + cv052 + "------PHONE----" + phone);
                if (page.equals("customerexpand.jsp")) {
                    redirect = "customerexpand.jsp?";
                } else if (page.equals("customer4tour.jsp")) {
                    V030controller v030controller = new V030controller();
                    redirect = "customer4tour.jsp?status=T&pv050=" + v030controller.getFV050() + "&action=delSS&linkA00=" + Util.convertMD5(getRequest().getRequestURI());

                } else if (page.equals("cuscompany.jsp")) {
                    long time11 = new Date().getTime();
                    redirect = "cuscompany.jsp?tab=itemcontent" + fk100 + "&flag=" + time11 + "&#" + fk100 + "";
                    //  redirect = "customer4tour.jsp?status=T&pv050=" + v030controller.getFV050() + "&action=delSS&linkA00=" + Util.convertMD5(getRequest().getRequestURI());

                } else if (page.equals("sendSMSitemEmployee")) {
                    if (SessionProvider.getSessionMapValue("IDSENDSMSMAIL") != null) {
                        fn850 = Util.parseInt(SessionProvider.getSessionMapValue("IDSENDSMSMAIL"));
                    }
                    if (fn850 == 0) {
                        fn850 = getLogin().getFn850();
                    }
                    redirect = "employeenew.jsp?actionn=reload&fn450=0&fn850=" + fn850 + "";
                } else if (page.equals("smsforV180")) {
                    V030controller v030 = new V030controller();
                    redirect = "customer4tour.jsp?status=T&pv050=" + v030.getFV050() + "&action=delSS&tab=upheader" + fk100 + "";
                } else if (page.equals("itemshowevenSMS")) {
                    redirect = "itemshowevent.jsp";
                }
                if (addnumbersms.length() > 0) {
                    String[] stringsms = addnumbersms.toString().split(",");
                    if (stringsms != null) {
                        for (int i = 0; i < stringsms.length; i++) {
                             if (flagncc == 2) {
                                  messageSMS = sendSmSUtil(cv051, stringsms[i].toString(),redirect);
                             }else{
                                  messageSMS = sendSmSUtilFibo(stringsms[i].toString(),cv051,redirect);
                             }
                           
                            int count = m020Store.stdm_tools_inserttabm020(pm020, cv052, cv051,"B", Util.toSQLDate(new Date()),
                                    Util.toSQLDate(new Date()), messageSMS, mn026, fm000, fv050, fn100,stringsms[i].toString(),"", getLogin().getUserName());
                        }
                    }
                }
                if (group == 1) {//gui 1 ng
                    if (flagncc == 2) {
                        System.out.println("------------NCC----BEYU");
                        SGTSendSMS(cv052, cv051, phone, fv050, fn100, redirect, 8, fm000);
                    } else {
                        System.out.println("------------NCC----FIBO");
                        SGTSendSMSFibo(cv052, cv051, phone, fv050, fn100, redirect, 8, fm000);
                    }

                } else {//gui nhom
                    if (sController.getListSMS().size() > 0) {
                        listSMSFinal = sController.getListSMS();
                        for (int i = 0; i < listSMSFinal.size(); i++) {
                            if (listSMSFinal.get(i).getNv958() == null) {
                                sendRedirect(redirect);
                            } else {
                                if (flagncc == 2) {
                                    System.out.println("------------NCC----BEYU");
                                    SGTSendSMS(cv052, cv051, listSMSFinal.get(i).getNv958(), fv050, listSMSFinal.get(i).getPn100(), redirect, mn026, fm000);
                                } else {
                                    System.out.println("------------NCC----FIBO");
                                    SGTSendSMSFibo(cv052, cv051, listSMSFinal.get(i).getNv958(), fv050, listSMSFinal.get(i).getPn100(), redirect, mn026, fm000);
                                }

                            }

                        }
                    }
                }
            } else {
                showGlobalReport(true, getResourceString("Nội dung trống!!!!"));
                sendRedirect(redirect);
            }

        } catch (Exception e) {
        }
    }

    private void SGTSendSMS(String cv052, String cv051, String phone, int fv050, int fn100, String sendRedirect, int mv026, int fm000) {
        try {
            System.out.println("----------GUI SMS--------BEYU-" + cv051 + phone);
            messageSMS = SMSUTIL.sendSmSUtil(cv051, phone);
            int count = m020Store.stdm_tools_inserttabm020(0, cv052, cv051,"B", Util.toSQLDate(new Date()),
                    Util.toSQLDate(new Date()), messageSMS, mv026, fm000, fv050, fn100,phone,"", getLogin().getUserName());
            System.out.println("m020Store.stdm_tools_inserttabm020--:" + count + messageSMS);
            if (messageSMS.equals("SMS SUCCESS")) {

                showGlobalReport(true, getResourceString("datasendsmssuccess"));
                sendRedirectUrlTab(sendRedirect);

            } else {
                showGlobalReport(true, getResourceString("datasendsmsUnsuccess"));
                sendRedirectUrlTab(sendRedirect);
            }
            if (SessionProvider.getSessionMapValue("listSelected") != null) {
                SessionProvider.removeSessionMapValue("listSelected");

            } else if (SessionProvider.getSessionMapValue("listSelectedEx") != null) {
                SessionProvider.removeSessionMapValue("listSelectedEx");

            }

        } catch (Exception e) {
        }

    }
    
    // ham tra vepagebeyu
      public  String sendSmSUtil(String message, String receiver,String sendRedirect) {
        String messageResult = null;
        AgentSmsApi_Service agent = new AgentSmsApi_Service();
        int type = 1; //1: notify message, 0: Advertise message
        String result = agent.getAgentSmsApiServiceImplPort().sendSms(PackageContants.authenticate_user, PackageContants.authenticate_pass, PackageContants.bran_name, message, receiver, type);
        System.out.println("-----------RESULT BEYU--------" + result);
        if (result != null) {
            if (result.equals("-13")) {
                messageResult = "Invaild receiver.";
            } else if (result.equals("-10")) {
                messageResult = "Missing username";
            } else if (result.equals("-11")) {
                messageResult = "Missing password";
            } else if (result.equals("-8")) {
                messageResult = "Missing brand name";
            } else if (result.equals("-7")) {
                messageResult = "Missing sms text";
            } else if (result.equals("-12")) {
                messageResult = "Message length too long to allowed";
            } else if (result.equals("-6")) {
                messageResult = "Missing destination address";
            } else if (result.equals("-5")) {
                messageResult = "Lỗi User";
            } else if (result.equals("-4")) {
                messageResult = "Invalid brand name";
            } else if (result.equals("-3")) {
                messageResult = "Invalid type";
            } else if (result.equals("-15")) {
                messageResult = "Not enough credits";
            } else if (result.equals("-20")) {
                messageResult = "Không đúng nội dung theo quy định";
            } else if (result.equals("-21")) {
                messageResult = "IP này không hỗ trợ gửi tin nhắn!";
            } else if (result.equals("-22")) {
                messageResult = "Qua thời gian gửi SMS";
            } else {
                int i = result.indexOf("|");
                if (i > 0) {
                    String reString = result.substring(0, i);
                    if (reString.equals("-14")) {
                        messageResult = "Message contain forbidden words.";
                    } else if (reString.equals("-9")) {
                        messageResult = "Message submit failed because the phone number invalid format.";
                    } else if (reString.equals("1")) {
                        messageResult = "SMS SUCCESS";
                    } else {
                        messageResult = "SMS NO SUCCESS";
                    }
                }
            }
        }
        if (messageResult.equals("SMS SUCCESS")) {
                showGlobalReport(true, getResourceString("datasendsmssuccess"));
                sendRedirectUrlTab(sendRedirect);

            } else {
                showGlobalReport(true, getResourceString("datasendsmsUnsuccess"));
                sendRedirectUrlTab(sendRedirect);
            }
            if (SessionProvider.getSessionMapValue("listSelected") != null) {
                SessionProvider.removeSessionMapValue("listSelected");

            } else if (SessionProvider.getSessionMapValue("listSelectedEx") != null) {
                SessionProvider.removeSessionMapValue("listSelectedEx");

            }
        return messageResult;

    }
    // send Fibo tra ve page
     public String sendSmSUtilFibo(String numberPhone, String contentmessage,String sendRedirect) {
        String messageResult = null;
        Service ser = new Service();
       
        String result = ser.getServiceSoap().sendSMS(PackageContants.clientNo, PackageContants.clientPass, numberPhone, contentmessage, PackageContants.smsGUID, Integer.parseInt(PackageContants.serviceType));
        if (result != null) { 
            
            int t = "<Code>".length();
            int n = result.trim().indexOf("<Code>");
            int m = result.trim().indexOf("</Code>");
            String a = result.substring(n + t, m);
           
            if(a.equals("200")){
                  messageResult = "SMS SUCCESS";
            }else if(a.equals("100")){
                 messageResult = "Please check your account!";
            }else if(a.equals("102")){
                 messageResult = "Your balance is zero!";
            }else if(a.equals("101")){
                 messageResult = "Please check your Phone number!";
            }else if(a.equals("103")){
                 messageResult = "Service type is wrong!";
            }
        }
        if (messageResult.equals("SMS SUCCESS")) {
                showGlobalReport(true, getResourceString("datasendsmssuccess"));
                sendRedirectUrlTab(sendRedirect);

            } else {
                showGlobalReport(true, getResourceString("datasendsmsUnsuccess"));
                sendRedirectUrlTab(sendRedirect);
            }
            if (SessionProvider.getSessionMapValue("listSelected") != null) {
                SessionProvider.removeSessionMapValue("listSelected");

            } else if (SessionProvider.getSessionMapValue("listSelectedEx") != null) {
                SessionProvider.removeSessionMapValue("listSelectedEx");

            }
        return messageResult;

    }

    private void SGTSendSMSFibo(String cv052, String cv051, String phone, int fv050, int fn100, String sendRedirect, int mv026, int fm000) {
        try {
            System.out.println("----------GUI SMS----FIBO-----" + cv051 + phone);
            messageSMS = SMSUTIL.sendSmSUtilFibo(phone, cv051);
            int count = m020Store.stdm_tools_inserttabm020(0, cv052, cv051,"B", Util.toSQLDate(new Date()),
                    Util.toSQLDate(new Date()), messageSMS, mv026, fm000, fv050, fn100,phone,"", getLogin().getUserName());
            System.out.println("m020Store.stdm_tools_inserttabm020---------------------FIBO:" + count + messageSMS);
            if (messageSMS.equals("SMS SUCCESS")) {
                showGlobalReport(true, getResourceString("datasendsmssuccess"));
                sendRedirectUrlTab(sendRedirect);

            } else {
                showGlobalReport(true, getResourceString("datasendsmsUnsuccess"));
                sendRedirectUrlTab(sendRedirect);
            }
            if (SessionProvider.getSessionMapValue("listSelected") != null) {
                SessionProvider.removeSessionMapValue("listSelected");

            } else if (SessionProvider.getSessionMapValue("listSelectedEx") != null) {
                SessionProvider.removeSessionMapValue("listSelectedEx");

            }

        } catch (Exception e) {
        }

    }

    /**
     * @return the pc050
     */
    public int getPc050() {
        return pc050;
    }

    /**
     * @param pc050 the pc050 to set
     */
    public void setPc050(int pc050) {
        this.pc050 = pc050;
    }

    /**
     * @return the cv051
     */
    public String getCv051() {
        return cv051;
    }

    /**
     * @param cv051 the cv051 to set
     */
    public void setCv051(String cv051) {
        this.cv051 = cv051;
    }

    /**
     * @return the cv052
     */
    public String getCv052() {
        return cv052;
    }

    /**
     * @param cv052 the cv052 to set
     */
    public void setCv052(String cv052) {
        this.cv052 = cv052;
    }

    /**
     * @return the list
     */
    public List<M020> getList() {
        try {

            list = m020Store.mcurpks_listoftabm020(getLogin().getModule(), getLogin().getCompanyNo(), 0, fv050, fn100,"B", Util.toSQLDate(md023), Util.toSQLDate(md024), mn026, getLogin().getUserName());
            return list;
        } catch (Exception e) {
            return null;
        }

    }

    /**
     * @param list the list to set
     */
    public void setList(List<M020> list) {
        this.list = list;
    }

    /**
     * @return the fv050
     */
    public int getFv050() {
        return fv050;
    }

    /**
     * @param fv050 the fv050 to set
     */
    public void setFv050(int fv050) {
        this.fv050 = fv050;
    }

    /**
     * @return the fn100
     */
    public int getFn100() {
        return fn100;
    }

    /**
     * @param fn100 the fn100 to set
     */
    public void setFn100(int fn100) {
        this.fn100 = fn100;
    }

    /**
     * @return the fm000
     */
    public int getFm000() {
        return fm000;
    }

    /**
     * @param fm000 the fm000 to set
     */
    public void setFm000(int fm000) {
        this.fm000 = fm000;
    }

    /**
     * @return the fm010
     */
    public int getFm010() {
        return fm010;
    }

    /**
     * @param fm010 the fm010 to set
     */
    public void setFm010(int fm010) {
        this.fm010 = fm010;
    }

    /**
     * @return the mv025
     */
    public String getMv025() {
        return mv025;
    }

    /**
     * @param mv025 the mv025 to set
     */
    public void setMv025(String mv025) {
        this.mv025 = mv025;
    }

    /**
     * @return the md023
     */
    public Date getMd023() {
        return md023;
    }

    /**
     * @param md023 the md023 to set
     */
    public void setMd023(Date md023) {
        this.md023 = md023;
    }

    /**
     * @return the md024
     */
    public Date getMd024() {
        return md024;
    }

    /**
     * @param md024 the md024 to set
     */
    public void setMd024(Date md024) {
        this.md024 = md024;
    }

    /**
     * @return the pm020
     */
    public int getPm020() {
        return pm020;
    }

    /**
     * @param pm020 the pm020 to set
     */
    public void setPm020(int pm020) {
        this.pm020 = pm020;
    }

    /**
     * @return the listSMSFinal
     */
    public List<N100> getListSMSFinal() {
        try {
        } catch (Exception e) {
        }
        return listSMSFinal;
    }

    /**
     * @param listSMSFinal the listSMSFinal to set
     */
    public void setListSMSFinal(List<N100> listSMSFinal) {
        this.listSMSFinal = listSMSFinal;
    }

    /**
     * @return the messageSMS
     */
    public String getMessageSMS() {
        return messageSMS;
    }

    /**
     * @param messageSMS the messageSMS to set
     */
    public void setMessageSMS(String messageSMS) {
        this.messageSMS = messageSMS;
    }

    /**
     * @return the phone
     */
    public String getPhone() {
        return phone;
    }

    /**
     * @param phone the phone to set
     */
    public void setPhone(String phone) {
        this.phone = phone;
    }

    /**
     * @return the hoten
     */
    public String getHoten() {
        return hoten;
    }

    /**
     * @param hoten the hoten to set
     */
    public void setHoten(String hoten) {
        this.hoten = hoten;
    }

    /**
     * @return the nameTour
     */
    public String getNameTour() {
        return nameTour;
    }

    /**
     * @param nameTour the nameTour to set
     */
    public void setNameTour(String nameTour) {
        this.nameTour = nameTour;
    }

    /**
     * @return the dateTour
     */
    public String getDateTour() {
        return dateTour;
    }

    /**
     * @param dateTour the dateTour to set
     */
    public void setDateTour(String dateTour) {
        this.dateTour = dateTour;
    }

    /**
     * @return the flag
     */
    public int getFlag() {
        return flag;
    }

    /**
     * @param flag the flag to set
     */
    public void setFlag(int flag) {
        this.flag = flag;
    }

    /**
     * @return the countchar
     */
    public int getCountchar() {
        if (cv051 != null) {
            countchar = 320 - cv051.length();
        }
        return countchar;
    }

    /**
     * @param countchar the countchar to set
     */
    public void setCountchar(int countchar) {
        this.countchar = countchar;
    }

    /**
     * @return the numberSMS
     */
    public int getNumberSMS() {
        System.out.println("---------VAO lay so nguoi -------------------"+SessionProvider.getSessionMapValue("listSelected"));
        if (SessionProvider.getSessionMapValue("listSelected") != null) {
            List<N100> listSMS = (List<N100>) SessionProvider.getSessionMapValue("listSelected");
            System.out.println("----------LIST SMS---" + listSMS.size());
            numberSMS = listSMS.size();
        } else if (SessionProvider.getSessionMapValue("listPersonalDSVN") != null) {
            List<N100> listSMS = (List<N100>) SessionProvider.getSessionMapValue("listPersonalDSVN");
            numberSMS = listSMS.size();
        } else if (SessionProvider.getSessionMapValue("listPersonalDSDD") != null) {
            List<N100> listSMS = (List<N100>) SessionProvider.getSessionMapValue("listPersonalDSDD");
            numberSMS = listSMS.size();
        } else if (SessionProvider.getSessionMapValue("listPersonalDSDD") != null) {
            List<N100> listSMS = (List<N100>) SessionProvider.getSessionMapValue("listPersonalDSDD");
            numberSMS = listSMS.size();
        } else if (SessionProvider.getSessionMapValue("listSelectedEx") != null) {
            List<N100> listSMS = (List<N100>) SessionProvider.getSessionMapValue("listSelectedEx");
            System.out.println("----------LIST SMS1---" + listSMS.size());
            numberSMS = listSMS.size();
        } else if (SessionProvider.getSessionMapValue("listSelectedKH") != null) {
            List<N100> listSMS = (List<N100>) SessionProvider.getSessionMapValue("listSelectedKH");
        }
        return numberSMS;
    }

    /**
     * @param numberSMS the numberSMS to set
     */
    public void setNumberSMS(int numberSMS) {
        this.numberSMS = numberSMS;
    }

    /**
     * @return the numberflag
     */
    public int getNumberflag() {
        return numberflag;
    }

    /**
     * @param numberflag the numberflag to set
     */
    public void setNumberflag(int numberflag) {
        this.numberflag = numberflag;
    }

    /**
     * @return the group
     */
    public int getGroup() {
        return group;
    }

    /**
     * @param group the group to set
     */
    public void setGroup(int group) {
        this.group = group;
    }

    /**
     * @return the page
     */
    public String getPage() {
        return page;
    }

    /**
     * @param page the page to set
     */
    public void setPage(String page) {
        this.page = page;
    }

    /**
     * @return the fk100
     */
    public int getFk100() {
        return fk100;
    }

    /**
     * @param fk100 the fk100 to set
     */
    public void setFk100(int fk100) {
        this.fk100 = fk100;
    }

    /**
     * @return the fn850
     */
    public int getFn850() {

        return fn850;
    }

    /**
     * @param fn850 the fn850 to set
     */
    public void setFn850(int fn850) {
        this.fn850 = fn850;
    }

    /**
     * @return the fv150
     */
    public int getFv150() {
        return fv150;
    }

    /**
     * @param fv150 the fv150 to set
     */
    public void setFv150(int fv150) {
        this.fv150 = fv150;
    }

    /**
     * @return the mn026
     */
    public int getMn026() {
        return mn026;
    }

    /**
     * @param mn026 the mn026 to set
     */
    public void setMn026(int mn026) {
        this.mn026 = mn026;
    }

    /**
     * @return the addnumbersms
     */
    public String getAddnumbersms() {
        return addnumbersms;
    }

    /**
     * @param addnumbersms the addnumbersms to set
     */
    public void setAddnumbersms(String addnumbersms) {
        this.addnumbersms = addnumbersms;
    }

    /**
     * @return the flagncc
     */
    public int getFlagncc() {
        return flagncc;
    }

    /**
     * @param flagncc the flagncc to set
     */
    public void setFlagncc(int flagncc) {
        this.flagncc = flagncc;
    }

    public void changeSuppliersms() {
        try {
            System.out.println("-------------CHON NCC--------" + flagncc);
        } catch (Exception e) {
        }
    }

    /**
     * @return the namepage
     */
    public String getNamepage() {
        return namepage;
    }

    /**
     * @param namepage the namepage to set
     */
    public void setNamepage(String namepage) {
        this.namepage = namepage;
    }
}
